package com.eqdd.yiqidian.ui.mine.setting;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.AddFeedbackActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditChooseBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.eqdd.yiqidian.ui.select.SelectRichTextActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddFeedbackActivity extends SlimBaseMultiEditActivity {
    private AddFeedbackActivityCustom dataBinding;
    private RichTextResult richTextResult;
    private String msg;
    private Map<String ,Integer> maps=new HashMap<>();


    @Override
    public void initBinding() {
        maps.put("bug",0);
        maps.put("意见",1);
        maps.put("账号申诉",2);
        isRead = false;
        isSave = false;
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_feedback);
        initTopTitleBar(View.VISIBLE,"添加反馈");
        initTopRightText("提交", v -> {
            if (checkParam()) {
                PostRequest<HttpResult> params = OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_FEEDBACK)
                        .params("title", setParam(0))
                        .params("contactway", setParam(1))
                        .params("type", maps.get(setParam(2)))
                        .params("userGuid", user.getGuid());
                if (richTextResult != null) {
                    params.params("fbcontent", richTextResult.getContent());
                    List<LocalMedia> selectMedia = richTextResult.getSelectMedia();
                    for (int i = 0; i < selectMedia.size(); i++) {
                        params.params("file", new File(selectMedia.get(i).isCompressed() ? selectMedia.get(i).getCompressPath() : selectMedia.get(i).getPath()));
                    }
                } else {
                    params.params("fbcontent", "");
                }
                params.execute(new DialogCallBack<HttpResult>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            setResult(Config.SUCCESS);
                            finish();
                        }
                    }
                });

            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(setParam(0))) {
            msg = "请填写标题";
            return false;
        }
        if (TextUtils.isEmpty(setParam(1))) {
            msg = "请填写联系方式";
            return false;
        }
        if (TextUtils.isEmpty(setParam(2))) {
            msg = "请选择反馈类型";
            return false;
        }
        return true;
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {

        switch (datas.get(p).getPosition()) {
            case 3:
                Bundle bundle = new Bundle();
                bundle.putString(Config.TITLE, "反馈内容");
                bundle.putParcelable(Config.RICH_TEXT_RESULT, richTextResult);
                startActivityForResult(SelectRichTextActivity.class, bundle, Config.SELECT_RICH_TEXT);
                break;
        }
    }


    @Override
    protected void setEditBeans() {

        datas.add(new SlimEditEnterBean("标题", "", 0, "请输入"));
        datas.add(new SlimEditEnterBean("联系方式", user.getUname(), 1, "请输入").setTip("手机或邮箱"));
        datas.add(new SlimEditChooseBean("反馈类型", "", 2, "请选择", new String[]{"bug", "意见", "账号申诉"}));
        datas.add(new SlimEditEnterBean("反馈内容", "", 3, true, "请输入"));

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.SUCCESS && requestCode == Config.SELECT_RICH_TEXT) {

            richTextResult = (RichTextResult) data.getParcelableExtra(Config.RICH_TEXT_RESULT);
            slimAdapterEx.getDataItem(3).setContent(richTextResult.getContent());
            slimAdapterEx.notifyItemChanged(3);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
